<?php

/**
 * Subclass for performing query and update operations on the 'articulo' table.
 *
 * 
 *
 * @package lib.model
 */ 
class ArticuloPeer extends BaseArticuloPeer
{

	static function getAllReorden($pagina = 1){
		
		$c = new Criteria();
    $c->add(ArticuloPeer::EXISTENCIA, ArticuloPeer::EXISTENCIA.'<='.ArticuloPeer::REORDEN, Criteria::CUSTOM);
    $pager = new sfPropelPager('Articulo', 20);
    $pager->setCriteria( $c );
    $pager->setPage($pagina);
    $pager->init();
    return $pager;
	}

	static function getAll($pagina = 1 ){
		$pager = new sfPropelPager('Articulo', 20);
    $pager->setCriteria( new Criteria() );
    $pager->setPage($pagina);
    $pager->init();
    
		return $pager;
	}
	
  static function getArtByCategoria($cat , $orden = 1 , $pagina = 1){
    $c = new Criteria();
    if( $cat != 'todos')
    {
      $c->add(CategoriaPeer::NOMBRE, $cat );
      $c->addJoin(ArticuloPeer::CATEGORIA_ID, CategoriaPeer::ID);
    }
    switch($orden){
    	case 1:
    		$c->addAscendingOrderByColumn(ArticuloPeer::PRECIO);
    		break;
    	case 2:
    		$c->addDescendingOrderByColumn(ArticuloPeer::PRECIO);
    		break;
    	case 3:
    		$c->addDescendingOrderByColumn(ArticuloPeer::FECHA_COMPRA);
    }
    $pager = new sfPropelPager('Articulo', 20);
    $pager->setCriteria( $c );
    $pager->setPage($pagina);
    $pager->init();
    return $pager;
  }
  
  static function getArtBySearch($search, $orden = 1, $pagina = 1){
  	$c = new Criteria();
    $c->add(ArticuloPeer::DESCRIPCION,'%'.$search.'%',Criteria::LIKE);
    switch($orden){
      case 1:
        $c->addAscendingOrderByColumn(ArticuloPeer::PRECIO);
        break;
      case 2:
        $c->addDescendingOrderByColumn(ArticuloPeer::PRECIO);
        break;
      case 3:
        $c->addDescendingOrderByColumn(ArticuloPeer::FECHA_COMPRA);
    }
    $pager = new sfPropelPager('Articulo', 20);
    $pager->setCriteria( $c );
    $pager->setPage($pagina);
    $pager->init();
    return $pager;
  }
	
  static function getArtByUltimos(){
  	$c = new Criteria();
    //$c->add( ArticuloPeer::FECHA_COMPRA, date('Y-m-d') );
    $c->addAscendingOrderByColumn(ArticuloPeer::FECHA_COMPRA);
    $c->setLimit(4);
    return ArticuloPeer::doSelect($c);
  }
  
  static function getArtByVendidos(){
  	$conexion = Propel::getConnection();
  	$consulta = 'SELECT %s , %s, %s, %s, sum(%s) as CANTIDAD FROM %s ,%s WHERE %s = %s GROUP BY %s ORDER BY CANTIDAD';
  	$consulta = sprintf($consulta,
  	                               ArticuloPeer::ID,
  	                               ArticuloPeer::DESCRIPCION,
  	                               ArticuloPeer::FOTO,
  	                               ArticuloPeer::PRECIO,
  	                               ArticuloVentaPeer::CANTIDAD,
  	                               ArticuloPeer::TABLE_NAME,
  	                               ArticuloVentaPeer::TABLE_NAME,
  	                               ArticuloPeer::ID,
  	                               ArticuloVentaPeer::ARTICULO_ID,
  	                               ArticuloVentaPeer::ARTICULO_ID );
  	$sentencia = $conexion->prepareStatement($consulta);
  	return $sentencia->executeQuery();
  	
    
  }
  
  static function getRelacionados($id){
  	$c = new Criteria();
  	$c->addJoin(ArticuloPeer::ID,SugerenciaPeer::RELACIONADO);
    $c->add(SugerenciaPeer::ARTICULO_ID,$id);
    $c->setLimit(4);
    return ArticuloPeer::doSelect($c);
  }
  
	static function getTotal(){
		return ArticuloPeer::doCount(new Criteria);
	}
}
